// 订单服务实现类
package com.heima.service.impl;

import com.heima.entity.OrderInfo;
import com.heima.entity.SeckillOrder;
import com.heima.mapper.OrderMapper;
import com.heima.service.OrderService;
import com.heima.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Transactional
    public OrderInfo createOrder(long userId, GoodsVo goods) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(userId);
        orderInfo.setGoodsId(goods.getGoodsId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsPrice(goods.getSeckillPrice());
        orderInfo.setOrderChannel((byte)1);
        orderInfo.setStatus((byte)0);
        orderInfo.setCreateDate(new Date());
        orderMapper.insert(orderInfo);

        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(userId);
        seckillOrder.setOrderId(orderInfo.getId());
        seckillOrder.setGoodsId(goods.getGoodsId());
        orderMapper.insertSeckillOrder(seckillOrder);

        return orderInfo;
    }

    public SeckillOrder getSeckillOrderByUserIdAndGoodsId(long userId, long goodsId) {
        return orderMapper.getSeckillOrderByUserIdAndGoodsId(userId, goodsId);
    }
}
